/**
 * 一些不要混入（mixins）的工具
 *
 * @Author: baiyuxin
 * @Date: 2019-11-20
 * @Last Modified by: baiyuxin
 * @Last Modified time: 2019-11-20
 */

export const toNumber = (initForm, fieldName, val) => {
  if (val && !isNaN(Number(val))) {
    initForm.find(x => x.fieldName === fieldName).initialValue = Number(val);
  } else {
    initForm.find(x => x.fieldName === fieldName).initialValue = undefined;
  }
}
export const getTimeStr = (date) => {
  var year = date.getFullYear();
  var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
  var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}


export const isEmpty = (obj) => {
  if (typeof obj == "undefined" || obj == null || obj == "" || obj == 'null') {
    return true;
  } else {
    return false;
  }
}

export const cutOutNum = (num, decimals) => {
  if (isNaN(num) || (!num && num !== 0)) {
    return '--'
  }
  // 默认为保留的小数点后两位
  let dec = decimals ? decimals : 3
  let tempNum = Number(num)
  let pointIndex = String(tempNum).indexOf('.') + 1 // 获取小数点的位置 + 1
  let pointCount = pointIndex ? String(tempNum).length - pointIndex : 0 // 获取小数点后的个数(需要保证有小数位)
  // 源数据为整数或者小数点后面小于decimals位的作补零处理
  if (pointIndex === 0 || pointCount <= dec) {
    let tempNumA = tempNum
    if (pointIndex === 0) {
      tempNumA = `${tempNumA}.`
      for (let index = 0; index < dec - pointCount; index++) {
        tempNumA = `${tempNumA}0`
      }
    } else {
      for (let index = 0; index < dec - pointCount; index++) {
        tempNumA = `${tempNumA}0`
      }
    }
    return tempNumA
  }
  let realVal = ''


  console.log("#!@#!@", `${String(tempNum)
    .split('.')[1]
    .substring(2, dec)}`)
  realVal = `${String(tempNum).split('.')[0]}.${String(tempNum)
    .split('.')[1]
    .substring(0, dec-1)}`
  // 截取当前数据到小数点后decimals位
  if (Number(`${String(tempNum)
  .split('.')[1]
  .substring(2, dec)}`) < 5) {
    console.log("1")
    return realVal;
  } else {
    console.log("2")
    return Number(realVal);
  }
}
